/*
 * xml_twitter.h
 *
 * Copyright (C) 2011 - Kellerman Rivero
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.ada
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef KEEJOO_SHARED_H
#define KEEJOO_SHARED_H
#include "keejoo_shared.h"
#endif

#ifndef API_TWITTER_H
#define API_TWITTER_H
#include "api_twitter.h"
#endif

#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>

#define MODULE_NAME "xml_twitter.h"

xmlDocPtr build_xml_tree_from_memory (char *xmlresource, long int length){
	xmlDocPtr resource;
	resource = xmlReadMemory(xmlresource,length,"noname.xml",NULL,0);
	if(resource == NULL){
		if(DEBUG_MODE){
			debug_print (MODULE_NAME,"build_xml_tree_from_memory","resource","Failed!");			
		}		
	}else{
		if(DEBUG_MODE){
			debug_print (MODULE_NAME,"build_xml_tree_from_memory","resource","Loaded!");			
		}	
	}
	return resource;
}

xmlNodeSetPtr eval_xpath_expression (xmlDocPtr document,const xmlChar* expression){
	xmlXPathContextPtr Context; 
    xmlXPathObjectPtr Object;
	Context = xmlXPathNewContext(document);

	if(Context==NULL) {
		if(DEBUG_MODE) debug_print(MODULE_NAME,"eval_xpath_expression","Error!","Context wont created");
		return NULL;
	}else{
		Object = xmlXPathEvalExpression(expression, Context);
		if(Object==NULL){
			if(DEBUG_MODE) debug_print(MODULE_NAME,"eval_xpath_expression","Error!", "Bad Expression Or Not Possible to evaluate!");
			return NULL;
			
		}else{
			return Object->nodesetval;
		}
	}
	
}
#ifdef DEBUG_MODE
void print_xpath_nodes (xmlNodeSetPtr nodes){
	xmlNodePtr cur;
    int size;
    int i;
    size = (nodes) ? nodes->nodeNr : 0;
    for(i = 0; i < size; ++i) {
		if(nodes->nodeTab[i]->type == XML_ELEMENT_NODE) {
			cur = nodes->nodeTab[i];   	    
			if(cur->ns) { 
			        printf("= element node \"%s: %s\"  ", cur->ns->href, cur->name);
			} else {
			        printf("= element node \"%s\"  ", cur->name);
			}
			printf("%s\n",xmlNodeGetContent(cur));
			get_profile_image ((char*)xmlNodeGetContent (cur));
		} else {
			cur = nodes->nodeTab[i];    
			printf("= node \"%s\": type %d", cur->name, cur->type);
			printf("%s\n",xmlNodeGetContent(cur));
		}
    }
}
#endif